//
// 作者：AKA_GGbond
// 时间：2025/7/16
// 邮箱：evain_wl@163.com
//

#include "W_Login.h"
#include <QPropertyAnimation>
#include <QPainter>
#include <QIntValidator>
#include <QApplication>
#include <QGridLayout>
#include <QWidget>
#include "W_Sql.h"
#include "W_ResponsiveForm.h"
#include "W_RadiusForm.h"
#include "W_CusWidget.h"

W_Login::W_Login(QWidget *parent)
    : QWidget{parent}
{
    this->resize(477, 620);

    m_loginButton = new W_LoginButton(this);
    m_loginButton->setCenter_text("登录");
    m_loginButton->move(46, 371);
    connect(m_loginButton, &W_LoginButton::execute_animation_signal, this, &W_Login::execute_animation);
    connect(m_loginButton,&W_LoginButton::clicked,this,&W_Login::on_loginButtonClicked);

    m_quitButton = new W_LoginButton(this);
    m_quitButton->setCenter_text("退出");
    m_quitButton->move(46, 481);
    m_quitButton->setbgColor(QColor(243,152,157));
    connect(m_quitButton,&W_LoginButton::clicked,this,[](){
        qApp->quit();
    });

    m_username = new W_InputBox(":/Img/account.png", this);
    m_username->move(46, 161);
    m_username->setPlaceholderText("Username");
    m_username->setMaxLength(11);
//    m_username->setValidator(new QIntValidator(0, 9999999999, this));

    m_password = new W_InputBox(":/Img/password.png", this);
    m_password->move(46, 253);
    m_password->setPlaceholderText("Password");
    m_password->setEchoMode(QLineEdit::Password);
    m_password->setMaxLength(16);
    m_password->setValidator(new QRegularExpressionValidator(QRegularExpression("[a-zA-Z0-9]+$"), this));

    this->animations();
}

W_Login::~W_Login()
{

}

void W_Login::execute_animation(W_LoginButton::AnimationState State)
{
    if (State == W_LoginButton::Execute)
    {
        m_animation->setDirection(QAbstractAnimation::Forward);
        m_animation->start();
    }
    else if (State == W_LoginButton::Restore)
    {
        m_animation->setDirection(QAbstractAnimation::Backward);
        m_animation->start();
    }
}

void W_Login::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    painter.setViewport(0, 0, 477, 620);
    painter.setWindow(0, 0, 477, 620);

    this->cropCorner();
    this->drawText();
}

void W_Login::cropCorner()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    QBrush Brush(QColor(255, 255, 255, 255));
    painter.setBrush(Brush);
    painter.drawRect(0, 0, width(), height());
}

void W_Login::drawText()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);

    QRect rect1(0, 0, 0, 0);
    QFont font1;
    font1.setPointSize(30);
    font1.setBold(true);
    font1.setWordSpacing(1);
    painter.setFont(font1);

    QColor semiTransparent(0, 0, 0, 255);
    painter.setPen(semiTransparent);

    QRect actualRect = painter.boundingRect(rect1, Qt::AlignCenter, "登录");
    rect1.setHeight(actualRect.height());
    rect1.setWidth(actualRect.width());
    rect1.moveCenter(QPoint(width() / 2, height() / 6));
    painter.drawText(rect1, "登录");
}

void W_Login::animations()
{
    m_animation = new QPropertyAnimation(this->m_loginButton, "geometry");
    m_animation->setDuration(250);
    m_animation->setStartValue(this->m_loginButton->geometry());
    m_animation->setEndValue(QRect(this->m_loginButton->pos().x() + m_zoom_rate, this->m_loginButton->pos().y() + m_zoom_rate / 2,
                                   m_loginButton->width() - m_zoom_rate * 2, m_loginButton->height() - m_zoom_rate));
    m_animation->setEasingCurve(QEasingCurve::Linear);
}

void W_Login::on_loginButtonClicked()
{
//比对密码，登录
    QString username = m_username->text();
    QString passwd = m_password->text();
    QString search_sql =  QString("SELECT CASE WHEN EXISTS (SELECT 1 FROM UserInfoModel WHERE userName = '%1' AND userPasswd = '%2') THEN 1 ELSE 0 END AS user_exists")
            .arg(username).arg(passwd);
    bool ret = W_SqlIns->Search(search_sql);
    if(!ret) {
        return;
    }

//    //赋值用户权限
//    if(username.compare("超级用户") == 0)
//    {
//        g_user_type = UserType_Root;
//    }
//    else if(username.compare("普通用户") == 0)
//    {
//        g_user_type = UserType_Normal;
//    }
//
//    //初始化主界面
//    delete m_mainWindow;
//    m_mainWindow = nullptr;
//    m_mainWindow = new MainWindow();
//    //打开主界面
    QWidget *w = new QWidget();
    w->resize(800,600);
    w->setObjectName("WMainWindow");
    QString style =
        "background: qlineargradient("
        "spread:pad, x1:0, y1:0, x2:1, y2:0,"
        "stop:0 rgb(15, 12, 41),"
        "stop:0.5 rgb(48, 43, 99),"
        "stop:1 rgb(36, 36, 62)"
        ");";

    w->setStyleSheet(style);
    //w->setStyleSheet("#WMainWindow{border-image:url(:/Img/bg.jpg);}");


    QGridLayout *layout = new QGridLayout(w);
    layout->addWidget(new W_RadiusForm,0,0);
    layout->addWidget(new W_CusWidget,1,0);
    layout->addWidget(new W_CusWidget,0,1);
    layout->addWidget(new W_RadiusForm,1,1);

    w->show();

    W_ResponsiveFormIns->close();
}
